package cn.itcast.n4_共享模型_管程;

import lombok.extern.slf4j.Slf4j;

import static cn.itcast.util.Sleeper.sleep;

/**
 * @program: juc
 * @description:
 * @author: flyduck
 * @create: 2024-09-27 16:31
 **/
@Slf4j(topic = "c.Test21_wait和notify方法_notify和notifyAll方法的区别")
public class Test21_wait和notify方法_notify和notifyAll方法的区别 {
    final static Object lock = new Object();

    public static void main(String[] args) {

        new Thread(() -> {
            synchronized (lock) {
                log.debug("执行....");
                try {
                    lock.wait(); // 让线程在obj上一直等待下去
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                log.debug("其它代码....");
            }
        },"t1").start();

        new Thread(() -> {
            synchronized (lock) {
                log.debug("执行....");
                try {
                    lock.wait(); // 让线程在obj上一直等待下去
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                log.debug("其它代码....");
            }
        },"t2").start();

        // 主线程两秒后执行
        sleep(0.5);
        log.debug("唤醒 obj 上其它线程");
        synchronized (lock) {
            lock.notify(); // 唤醒obj上一个线程
            //lock.notifyAll(); // 唤醒obj上所有等待线程
        }
    }
}
